#!/bin/bash
#
# For all script types, returning an exit code of 0 ( success ) means the
# script execution completed successfully.
#
# If this is a requirements script, returning any other exit code but 0 will be
# reported as a "Requirements Failure: Script" in the Client Info window and
# Fileset Report. These same non-zero exit code (e.g. 1 or -1) will also
# prevent the contents of the fileset from downloading and installing.
#
# For other types of scripts, any non-zero exit code (e.g. 1 or -1) causes the
# fileset installation to fail and a script failure to be reported.
#
# If the script finishes without returning an exit code, the exit code 0
# ( success ) is assumed by default.
#
# Add the contents of your script below:

#!/bin/bash

# Needs to be run after installing Chrome.  
# Enables the ksadmin process for updating.  
# Must be enabled for silent disabling of auto updates
# Updates can be disabled through Configuration Profile
# See https://www.chromium.org/administrators/policy-list-3#DeviceAutoUpdateDisabled

# Bundle short version string
# Example: 66.0.3359.139
chrome_shortverstring=$(defaults read /Applications/Google\ Chrome.app/Contents/Info.plist CFBundleShortVersionString)

# Updater URL
# Example: https://tools.google.com/service/update
chrome_ksupdateurl=$(defaults read /Applications/Google\ Chrome.app/Contents/Info.plist KSUpdateURL)

# Product ID
# Example com.google.Chrome
chrome_ksproductid=$(defaults read /Applications/Google\ Chrome.app/Contents/Info.plist KSProductID)

# ksadmin binary.  Needs to be installed using ksinstall
ksadmin_bin="/Library/Google/GoogleSoftwareUpdate/GoogleSoftwareUpdate.bundle/Contents/MacOS/ksadmin"

# Path to ksinstall
ksinstall_bin=$(find /Applications/Google\ Chrome.app -name ksinstall)
# Path to keystone.tbz file
keystone_tbz=$(find /Applications/Google\ Chrome.app -name Keystone.tbz)

# Instal ksadmin
"$ksinstall_bin" --install "$keystone_tbz" --force

# Delete Chrome from keystone if there is one 
"$ksadmin_bin" --delete --productid "$chrome_ksproductid" 2 >/dev/null

# Re-register Chrome with keystone
"$ksadmin_bin"  --register \
                --preserve-tttoken \
                --productid "$chrome_ksproductid" \
                --version "$chrome_shortverstring" \
                --xcpath "/Applications/Google Chrome.app" \
                --url "$chrome_ksupdateurl" \
                --tag-path "/Applications/Google Chrome.app/Contents/Info.plist" \
                --tag-key "KSChannelID" \
                --brand-path "/Library/Google/Google Chrome Brand.plist" \
                --brand-key "KSBrandID" \
                --version-path "/Applications/Google Chrome.app/Contents/Info.plist" \
                --version-key "KSVersion"

exit 0
